package com.lemon.common.security.utils;

import cn.hutool.core.collection.CollUtil;
import com.lemon.common.core.constant.Constants;
import com.lemon.common.core.exception.UtilException;
import com.lemon.common.core.utils.SpringUtils;
import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.redis.service.RedisService;
import com.lemon.system.api.domain.SysDictData;

import java.util.Collection;
import java.util.List;

/**
 * 字典工具类
 * 
 * @author huangpeng
 */
public class DictUtils
{
    /**
     * 设置字典缓存
     * 
     * @param key 参数键
     * @param dictDatas 字典数据列表
     */
    public static void setDictCache(String key, List<SysDictData> dictDatas)
    {
        SpringUtils.getBean(RedisService.class).setCacheObject(getCacheKey(key), dictDatas);
    }

    /**
     * 获取字典缓存
     * 
     * @param key 参数键
     * @return dictDatas 字典数据列表
     */
    public static List<SysDictData> getDictCache(String key)
    {
        Object cacheObj = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(cacheObj))
        {
            return StringUtils.cast(cacheObj);
        }
        return null;
    }

    /**
     * 删除指定字典缓存
     * 
     * @param key 字典键
     */
    public static void removeDictCache(String key)
    {
        SpringUtils.getBean(RedisService.class).deleteObject(getCacheKey(key));
    }

    /**
     * 清空字典缓存
     */
    public static void clearDictCache()
    {
        Collection<String> keys = SpringUtils.getBean(RedisService.class).keys(Constants.SYS_DICT_KEY + "*");
        SpringUtils.getBean(RedisService.class).deleteObject(keys);
    }

    /**
     * 设置cache key
     * 
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey)
    {
        return Constants.SYS_DICT_KEY + configKey;
    }

    /**
     * 根据dictType,label 获取value
     * @Title: getDictValueByTypeAndLabel
     * @Description: 根据dictType,label 获取value
     * @param label
     * @param dictType
     * @return: String
     * @throws: 
     * @author: huangpeng
     * @Date: 2022/12/17 17:47
     */
    public static Long getDictValueByTypeAndLabel(String label, String dictType) {
        if (StringUtils.isNotBlank(dictType) && StringUtils.isNotBlank(label)) {
            List<SysDictData> dictDataList = getDictCache(dictType);
            if (CollUtil.isEmpty(dictDataList)) {
                throw new UtilException("系统不存在数据字典【" + dictType + "】");
            }
            for (SysDictData dictData : dictDataList) {
                if (dictType.equals(dictData.getDictType()) && label.equals(dictData.getDictLabel())) {
                    return dictData.getDictCode();
                }
            }
        }
        return null;
    }

    /**
     * 根据dictType获取label数组
     * @Title: getLabelArr
     * @Description: 根据dictType获取label数组
     * @param dictType
     * @return: String
     * @throws:
     * @author: huangpeng
     * @Date: 2022/12/17 18:21
     */
    public static String[] getLabelArr(String dictType) {
        String[] arrLabel = new String[0];
        if (StringUtils.isNotBlank(dictType)) {
            List<SysDictData> dictDataList = getDictCache(dictType);
            if (CollUtil.isEmpty(dictDataList)) {
                throw new UtilException("系统不存在数据字典【" + dictType + "】");
            }
            arrLabel = new String[dictDataList.size()];
            for (int i = 0; i < dictDataList.size(); i++) {
                arrLabel[i] = dictDataList.get(i).getDictLabel();
            }
            return arrLabel;
        }
        return arrLabel;
    }

    /**
     * 根据指定的dictType,value 获取 label
     * @Title: getDictLabelByTypeAndValue
     * @Description: 根据指定的dictType,value 获取 label
     * @param value
     * @param dictType
     * @return: String
     * @throws:
     * @author: huangpeng
     * @Date: 2022/12/17 18:54
     */
    public static String getDictLabelByTypeAndValue(String value, String dictType){
        if (StringUtils.isNotBlank(dictType) && StringUtils.isNotBlank(value)){
            List<SysDictData> dictDataList = getDictCache(dictType);
            if (CollUtil.isEmpty(dictDataList)) {
                throw new UtilException("系统不存在数据字典【" + dictType + "】");
            }
            for (SysDictData dictData : dictDataList){
                if (dictType.equals(dictData.getDictType()) && value.equals(dictData.getDictValue())){
                    return dictData.getDictLabel();
                }
            }
        }
        return null;
    }
}
